package org.ekstrabilet.web.beans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.ekstrabilet.game.beans.Game;
import org.ekstrabilet.ticket.beans.MacTicket;
import org.ekstrabilet.user.beans.User;
import org.ekstrabilet.user.logic.UserLogic;
import org.ekstrabilet.user.permissions.UserPermissions;

public class UserBean {
	private String login;
	private String password;
	private boolean logged;
	private User user;
	private String action;
	private User selectedUser;
	private String page;
	
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public boolean isLogged() {
		return logged;
	}
	public void setLogged(boolean logged) {
		this.logged = logged;
	}
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}
	
	public boolean getPermissions(){
		if(user == null)
			return false;
		return UserPermissions.checkPermissions(user.getType(), action);
	}
	
	public ArrayList<User> getUsers(){
		return new ArrayList<User>(UserLogic.getInstance().getUsers());
	}
	
	public User getSelectedUser() {
		return selectedUser;
	}
	
	public void setSelectedUser(User selectedUser) {
		this.selectedUser = selectedUser;
	}
	
	public void setPage(String page) {
		this.page = page;
	}
	
	public String logIn(){
		user= UserLogic.getInstance().login(login, password);
		if(user != null){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Logged in"));
			logged= true;
		}
		
		else
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Login failed"));
		return "home.xhtml?faces-redirect=true";	
	}
	
	public String logOut(){
		user= null;
		login= "";
		password= "";
		logged= false;
		
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("tickets", new ArrayList<MacTicket>());

		
		return "home.xhtml?faces-redirect=true";
	}
	
	public String redirect(){
		return page+"?faces-redirect=true";
	}
	
	public String edit(){
		if(UserLogic.getInstance().update(selectedUser)){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Edit succesful"));
			return "adminPane.xhtml?faces-redirect=true";
		}
		else
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Edit failed"));
		return null;
			
	}
	
	public String remove(){
		if(UserLogic.getInstance().delete(selectedUser)){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete succesful"));
			return "adminPane.xhtml?faces-redirect=true";
		}
		else
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete failed"));
		return null;
	}
	
	public String back(){
		return "adminPane.xhtml?faces-redirect=true";
	}
	
	public String register(){
		return "register.xhtml?faces-redirect=true";
	}
	
	

}
